﻿using Common.Logging;
using System;
using System.Reflection;
using System.Text;

namespace GPSChina.Platform.Quartz
{

    public static class Logger
    {
        private static readonly ILog _logRollingFile = LogManager.GetLogger("RollingFile");

        //////////////////////////////////////////////////////////////////////////////////////////

        public static void LogExpenditureOfTime(DateTime begin, DateTime end, string methodName)
        {
            var span = (end - begin);
            Trace(string.Format("{0}开销{1}ms", methodName, span.TotalMilliseconds));
        }

        //////////////////////////////////////////////////////////////////////////////////////////

        public static void Error(Exception exception, params object[] extendedProperties)
        {
            Error(GetExceptionMessage(exception), extendedProperties);
        }

        public static void Error(object message, params object[] extendedProperties)
        {
            WriteLog("Error", Assembly.GetCallingAssembly(), message, extendedProperties);
        }

        public static void Fatal(Exception exception, params object[] extendedProperties)
        {
            Fatal(GetExceptionMessage(exception), extendedProperties);
        }

        public static void Fatal(object message, params object[] extendedProperties)
        {
            WriteLog("Fatal", Assembly.GetCallingAssembly(), message, extendedProperties);
        }

        public static void Info(Exception exception, params object[] extendedProperties)
        {
            Info(GetExceptionMessage(exception), extendedProperties);
        }

        public static void Info(object message, params object[] extendedProperties)
        {
            WriteLog("Info", Assembly.GetCallingAssembly(), message, extendedProperties);
        }

        public static void Trace(Exception exception, params object[] extendedProperties)
        {
            Trace(GetExceptionMessage(exception), extendedProperties);
        }

        public static void Trace(object message, params object[] extendedProperties)
        {
            WriteLog("Trace", Assembly.GetCallingAssembly(), message, extendedProperties);
        }

        public static void Warn(Exception exception, params object[] extendedProperties)
        {
            Warn(GetExceptionMessage(exception), extendedProperties);
        }

        public static void Warn(object message, params object[] extendedProperties)
        {
            WriteLog("Warn", Assembly.GetCallingAssembly(), message, extendedProperties);
        }

        #region Private static methods.

        private static string GetExceptionMessage(Exception exception)
        {
            StringBuilder builder = new StringBuilder();
            if (exception != null)
            {
                builder.AppendFormat("{0}", exception.Message + Environment.NewLine);
                builder.AppendFormat("Stack Trace: {0};", exception.StackTrace + Environment.NewLine);
                while (exception.InnerException != null)
                {
                    exception = exception.InnerException;
                    builder.AppendFormat("Inner Exception: {0};", exception.Message + Environment.NewLine);
                    builder.AppendFormat("Inner Stack Trace: {0};", exception.StackTrace + Environment.NewLine);
                }
            }
            return builder.ToString();
        }

        private static string MergeMessage(string category, Assembly assembly, object message, params object[] extendedProperties)
        {
            var builder = new StringBuilder();
            //builder.Append(category + " ");
            //builder.Append(assembly + " ");
            if (message != null)
            {
                builder.Append("" + message.ToString());
            }

            if (extendedProperties != null && extendedProperties.Length > 0)
            {
                builder.AppendLine("。额外属性：");
                for (int i = 0; i < extendedProperties.Length; i += 2)
                {
                    if ((extendedProperties[i] != null))
                    {
                        builder.Append(extendedProperties[i].ToString() + ": ");
                        if ((i + 1) < extendedProperties.Length && (extendedProperties[i + 1] != null))
                        {
                            builder.AppendLine(extendedProperties[i + 1].ToString());
                        }
                        else
                        {
                            builder.AppendLine("--");
                        }
                    }
                }
            }
            return builder.ToString();
        }

        private static void WriteLog(string catagory, Assembly assembly, object message, params object[] extendedProperties)
        {
            message = MergeMessage(catagory, assembly, message, extendedProperties);
            string str = catagory;
            if (str != null)
            {
                if (!(str == "Trace"))
                {
                    if (str == "Info")
                    {
                        if (!_logRollingFile.IsInfoEnabled)
                        {
                            //throw new Exception("配置文件初始化出错，请检查配置是否正确");
                        }
                        _logRollingFile.Info(message);
                    }
                    else if (str == "Warn")
                    {
                        if (!_logRollingFile.IsWarnEnabled)
                        {
                            // throw new Exception("配置文件初始化出错，请检查配置是否正确");
                        }
                        _logRollingFile.Warn(message);
                    }
                    else if (str == "Error")
                    {
                        if (!_logRollingFile.IsErrorEnabled)
                        {
                            //throw new Exception("配置文件初始化出错，请检查配置是否正确");
                        }
                        _logRollingFile.Error(message);
                    }
                    else if (str == "Fatal")
                    {
                        if (!_logRollingFile.IsFatalEnabled)
                        {
                            // throw new Exception("配置文件初始化出错，请检查配置是否正确");
                        }
                        _logRollingFile.Fatal(message);
                    }
                }
                else
                {
                    if (!_logRollingFile.IsDebugEnabled)
                    {
                        // throw new Exception("配置文件初始化出错，请检查配置是否正确");
                    }
                    _logRollingFile.Debug(message);
                }
            }
        }

        #endregion
    }
}
